package jnpf.base.util;


import jnpf.exception.DataException;
import jnpf.util.JdbcUtil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;

/**
 *
 * @author JNPF开发平台组
 * @version V3.1.0
 * @copyright 引迈信息技术有限公司
 * @date 2021/3/12 15:31
 */
public class DbSyncUtil {


    /**
     * 同步表
     * @param connectionStringTo
     * @param result
     * @param table
     * @return
     * @throws DataException
     * @throws SQLException
     */
    public static String tableSync(Connection connectionStringTo, ResultSet result, String table) throws DataException, SQLException {
        //获得结果集结构信息,元数据
        ResultSetMetaData md = result.getMetaData();
        //获得列数
        int columnCount = md.getColumnCount();
        StringBuilder insert = new StringBuilder();
        StringBuilder value = new StringBuilder("( ");
        while (result.next()) {
            for (int i = 1; i <= columnCount; i++) {
                Object object = result.getObject(i) == null ? null : result.getObject(i);
                String dataValue = null;
                if(object!=null) {
                    dataValue=result.getString(i).replaceAll("'","''");
                }
                if (i != columnCount) {
                    if(object!=null) {
                        value.append("'" + dataValue + "', ");
                    }else {
                        value.append(dataValue + ", ");
                    }
                } else {
                    if(object!=null) {
                        value.append("'" + dataValue + "' ),(");
                    }else {
                        value.append(dataValue + " ),(");
                    }
                }
            }
        }
        value.deleteCharAt(value.length() - 1);
        value.deleteCharAt(value.length() - 1);
        insert.append("INSERT INTO " + table + " VALUES " + value);
        JdbcUtil.custom(connectionStringTo, insert.toString());
        return "ok";
    }

    public static String oracleTableSync(Connection connectionStringTo, ResultSet result, String table) throws DataException, SQLException {
        //获得结果集结构信息,元数据
        ResultSetMetaData md = result.getMetaData();
        //获得列数
        int columnCount = md.getColumnCount();
        StringBuilder insert = new StringBuilder();
        StringBuilder value = new StringBuilder("( ");
        while (result.next()) {
            for (int i = 1; i <= columnCount; i++) {
                Object object = result.getObject(i) == null ? null : result.getObject(i);
                String dataValue = null;
                if(object!=null) {
                    dataValue=result.getString(i).replaceAll("'","''");
                }
                if (i != columnCount) {
                    if(object!=null) {
                        if("date".equals(result.getMetaData().getColumnTypeName(i).toLowerCase())
                                ||result.getMetaData().getColumnTypeName(i).toLowerCase().contains("time")){

                            value.append("TO_DATE('" + dataValue + "', 'YYYY-MM-DD HH24:MI:SS'), ");
                        }else{
                            value.append("'" + dataValue + "', ");
                        }
                    }else {
                        value.append(dataValue + ", ");
                    }
                } else {
                    if(object!=null) {
                        if("date".equals(result.getMetaData().getColumnTypeName(i).toLowerCase())
                                ||result.getMetaData().getColumnTypeName(i).toLowerCase().contains("time")){

                            value.append("TO_DATE('" + dataValue + "', 'YYYY-MM-DD HH24:MI:SS')),(");
                        }else{
                            value.append("'" + dataValue + "' ),( ");
                        }
                    }else {
                        value.append(dataValue + " ),(");
                    }
                }
            }
        }
        String resValue=value.toString().substring(0,value.length()-3);
        insert.append("INSERT INTO " + table + " VALUES " + resValue);
        JdbcUtil.custom(connectionStringTo, insert.toString());
        return "ok";
    }
}
